<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Controller;

class Group extends Base
{
    // 新增权限接口
    public function groupAdd()
    {
        $data = input();//token name author1 author2

        if (empty($data['name'])) {
            apiReturn(201,'权限角色名称不能为空');
        }
        if (empty($data['author1'])) {
            apiReturn(202,'菜单权限不能为空');
        }

        $datas = array(
            'name'=>$data['name'],
            'date'=>time()
        );
        $group_id = db('crm_group')->insertGetId($datas);

        if (!empty($data['author1'])) {
            $author1_array = explode(',', $data['author1']);
            foreach ($author1_array as $key => $value) {
                $author_datas = array(
                    'group_id'=>$group_id,
                    'author'=>$value,
                    'type'=>1,
                    'date'=>time()
                );
                db('crm_group_author')->insert($author_datas);
            }
        }
        if (!empty($data['author2'])) {
            $author1_array = explode(',', $data['author2']);
            foreach ($author1_array as $key => $value) {
                $author_datas = array(
                    'group_id'=>$group_id,
                    'author'=>$value,
                    'type'=>2,
                    'date'=>time()
                );
                db('crm_group_author')->insert($author_datas);
            }
        }
        apiReturn(200,'新增权限成功');
    }

    // 权限列表接口
    public function groupList()
    {
        $data = input();//token

        $groupList = db('crm_group')->select();

        apiReturn(200,'获取权限列表接口成功',$groupList);
    }

    // 获取权限接口
    public function groupInfo()
    {
        $data = input();//token group_id

        $groupInfo = db('crm_group')->where('id',$data['group_id'])->find();
        $groupInfo['author1'] = db('crm_group_author')->where(array('group_id'=>$data['group_id'],'type'=>1))->order('date ASC')->select();
        $groupInfo['author2'] = db('crm_group_author')->where(array('group_id'=>$data['group_id'],'type'=>2))->order('date ASC')->select();
        $author1 = [];
        foreach ($groupInfo['author1'] as $key => $value) {
            $author1[] = $value['author'];
        }
        $groupInfo['author1'] = $author1;
        $author2 = [];
        foreach ($groupInfo['author2'] as $key => $value) {
            $author2[] = $value['author'];
        }
        $groupInfo['author2'] = $author2;
        apiReturn(200,'获取权限接口成功',$groupInfo);
    }

    // 修改权限接口
    public function groupEdit()
    {
        $data = input();//token group_id name author1 author2

        if (empty($data['name'])) {
            apiReturn(201,'权限角色名称不能为空');
        }
        
        db('crm_group')->where('id',$data['group_id'])->update(['name'=>$data['name']]);

        db('crm_group_author')->where('group_id',$data['group_id'])->delete();
        if (!empty($data['author1'])) {
            $author1_array = explode(',', $data['author1']);
            foreach ($author1_array as $key => $value) {
                $author_datas = array(
                    'group_id'=>$data['group_id'],
                    'author'=>$value,
                    'type'=>1,
                    'date'=>time()
                );
                db('crm_group_author')->insert($author_datas);
            }
        }
        if (!empty($data['author2'])) {
            $author1_array = explode(',', $data['author2']);
            foreach ($author1_array as $key => $value) {
                $author_datas = array(
                    'group_id'=>$data['group_id'],
                    'author'=>$value,
                    'type'=>2,
                    'date'=>time()
                );
                db('crm_group_author')->insert($author_datas);
            }
        }
        apiReturn(200,'修改权限成功');
    }

    // 删除权限接口
    public function groupDelete()
    {
        $data = input();//token group_id

        db('crm_group')->where('id',$data['group_id'])->delete();
        db('crm_group_author')->where('group_id',$data['group_id'])->delete();
        apiReturn(200,'删除权限成功');
    }

}
